<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Model;
use PHPExcel;
use PHPExcel_IOFactory;
//个人目标
class Mymubiao extends Yusuandaoru
{
     //删除
    public function shanchu($tid){
        $tag=Db::table("target_apply")->where("id=$tid")->find();
        Db::startTrans();
        try{
           if($tag["state2"]==2){
                //异动
                Db::table("target_apply")->where(["id"=>$tag["yuanid"]])->update(["tag"=>0,"nextid"=>null]);
            } 
            $res=Db::table("target_apply")->where(["id"=>$tid])->update(["is_deleted"=>1]);
        }catch(\Exception $e){
            Db::rollBack();
            return 2;
        }
        if($res){
            Db::commit();
            return 1;
        }
        else{
              Db::rollBack();
            return 2;
        }
    }
	//我的发运目标汇总
	public function myfayuntongji($page=1,$list=20){
		//获取编号
		$typename="个人发运目标格式";
		$tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
		$type_id=$tmp["id"];
		//接收数据
    	$request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
        $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid  and t.tag<>1 ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) and t.tag<>1 ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid and t.tag<>1 ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and  t.state=7 and t.is_deleted=0 and t.type_id=$type_id and t.tag<>1 ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);

       $data=$request->post();
    	  if($data){
            if(trim($data["cainian"]))
                $where.="and ap.cainian='{$data["cainian"]}' ";
           if(trim($data["yijikaoheqi"]))
                $where.="and ap.yijikaoheqi='{$data["yijikaoheqi"]}' ";
            if(trim($data["erjikaoheqi"]))
                $where.="and ap.erjikaoheqi='{$data["erjikaoheqi"]}' ";
            if(trim($data["pinpai"]))
                $where.="and ap.pinpai='{$data["pinpai"]}' ";
            if(trim($data["yijifeiyong"]))
                $where.="and ap.yijifeiyong='{$data["yijifeiyong"]}' ";
            if(trim($data["erjifeiyong"]))
                $where.="and ap.erjifeiyong='{$data["erjifeiyong"]}' ";
             if(trim($data["channel1"]))
                $where.="and ap.channel1='{$data["channel1"]}' ";
             if(trim($data["fenqu"]))
                $where.="and ap.fenqu='{$data["fenqu"]}' ";
            if(trim($data["sheng"]))
                $where.="and ap.sheng='{$data["sheng"]}' ";
            if(trim($data["shi"])){
                $where.="and ap.shi='{$data["shi"]}' ";
            }
          
        }else{
            $data=["cainian"=>"","yijikaoheqi"=>"","erjikaoheqi"=>"","pinpai"=>"","yijifeiyong"=>""
                    ,"erjifeiyong"=>"","channel1"=>"","fenqu"=>"","sheng"=>"","shi"=>""
            ];
        }
        //echo $where;
        $sum=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        
        ->where($where)
        ->count();

        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        ->join("cg_jxs j","ap.jingxiao=j.id","LEFT")
        ->join("cg_market_zoning z","ap.fenqu=z.id","LEFT")
        ->join("cg_market_provice p","ap.sheng=p.id","LEFT")
        ->join("cg_market_city c","ap.shi=c.id","LEFT")
         ->join("cg_channel_types1 c1","ap.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","ap.channel2=c2.id","LEFT")
        ->join("employee e","ap.people=e.id","LEFT")
        ->join("cg_system x","ap.xitong=x.id","LEFT")
        ->join("cg_net n","ap.wangdian=n.id","LEFT")
        ->join("pro_brands pb","ap.pinpai=pb.id","LEFT")
        ->join("pro_category pc","ap.pinlei=pc.id","LEFT")
        ->join("pro_series ps","ap.xilie=ps.id","LEFT")
        ->join("cg_feiyongkemu1 f1","ap.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","ap.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","ap.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","ap.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","ap.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","ap.erjikaoheqi=k2.id","LEFT")
        ->where($where)
        ->order("t.id desc,ap.id desc")
        ->limit($start,$list)
        ->field("c1.title as channel1,z.title as fenqu,p.title as sheng,c.title as shi,j.title as jingxiao,pb.brand_name as pinpai,f1.title as  yijifeiyong,f2.title as erjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,ap.feiyong,ap.detail_no,t.no,pc.category_name as pinlei,e.em_name as ename")
        ->select();
        //print_r($apply);
         $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        //所有财年
        $cainian=Db::table("cg_cainian")->select();
        $this->assign("cainian",$cainian);
        //一级考核期
        $kaohe1=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.title,k1.id")->select();
        $this->assign("kaohe1",$kaohe1);
        $jsonkaohe1=json_encode($kaohe1);
        $this->assign("jsonkaohe1",$jsonkaohe1);
         $kaohe2=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_kaoheqi2 k2","k2.pid=k1.id")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.id as yijikaoheqi,k2.title,k2.id")->select();
          $this->assign("kaohe2",$kaohe2);
           $jsonkaohe2=json_encode($kaohe2);
          $this->assign("jsonkaohe2",$jsonkaohe2);
          //print_r($kaohe2);
        //品牌
        $pinpai=Db::table("pro_brands")->where(["is_deleted"=>0])->select();
        $this->assign("pinpai",$pinpai);
        //一级费用科目
        $yijifeiyong=Db::table("cg_feiyongkemu1")->select();
        $this->assign("yijifeiyong",$yijifeiyong);
        //二级费用科目
        $erjifeiyong=Db::table("cg_feiyongkemu2")->alias("f2")
        ->join("cg_feiyongkemu1 f1","f2.pid=f1.id")
        ->field("f1.id as yijifeiyong,f2.title,f2.id")
        ->select();
        $this->assign("erjifeiyong",$erjifeiyong);
        $jsonerjifeiyong=json_encode($erjifeiyong);
        $this->assign("jsonerjifeiyong",$jsonerjifeiyong);
        //渠道
        $qudao=Db::table("cg_channel_types1")->select();
        $this->assign("qudao",$qudao);
         //分区
        $fenqu=Db::table("cg_market_zoning")->where(["isdel"=>0])->select();
        $this->assign("fenqu",$fenqu);
        //省份
        $sheng=Db::table("cg_market_provice")->alias("p")
        ->join("cg_market_zoning z","p.zoning=z.id")
        ->field("z.id as fenqu,p.title,p.id")->select();
        $this->assign("sheng",$sheng);
       
         $jsonsheng=json_encode($sheng);
          $this->assign("jsonsheng",$jsonsheng);
        //市
        $shi=Db::table("cg_market_city")->alias("c")
        ->join("cg_market_provice p","c.provice=p.id")
        ->field("p.id as sheng,c.title,c.id")->select();
        $this->assign("shi",$shi);
        $jsonshi=json_encode($shi);
          $this->assign("jsonshi",$jsonshi);
		return $this->fetch('myfayuntongji');
	}
    //我的发运目标明细
    public function myfayunmingxi($page=1,$list=20){
        //获取编号
        $typename="个人发运目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        //接收数据
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
        $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid  and t.tag<>1 ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) and t.tag<>1 ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid and t.tag<>1 ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and  t.state=7 and t.is_deleted=0 and t.type_id=$type_id and t.tag<>1 ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);

       $data=$request->post();
          if($data){
            if(trim($data["cainian"]))
                $where.="and ap.cainian='{$data["cainian"]}' ";
           if(trim($data["yijikaoheqi"]))
                $where.="and ap.yijikaoheqi='{$data["yijikaoheqi"]}' ";
            if(trim($data["erjikaoheqi"]))
                $where.="and ap.erjikaoheqi='{$data["erjikaoheqi"]}' ";
            if(trim($data["pinpai"]))
                $where.="and ap.pinpai='{$data["pinpai"]}' ";
            if(trim($data["yijifeiyong"]))
                $where.="and ap.yijifeiyong='{$data["yijifeiyong"]}' ";
            if(trim($data["erjifeiyong"]))
                $where.="and ap.erjifeiyong='{$data["erjifeiyong"]}' ";
             if(trim($data["channel1"]))
                $where.="and ap.channel1='{$data["channel1"]}' ";
             if(trim($data["fenqu"]))
                $where.="and ap.fenqu='{$data["fenqu"]}' ";
            if(trim($data["sheng"]))
                $where.="and ap.sheng='{$data["sheng"]}' ";
            if(trim($data["shi"])){
                $where.="and ap.shi='{$data["shi"]}' ";
            }
          
        }else{
            $data=["cainian"=>"","yijikaoheqi"=>"","erjikaoheqi"=>"","pinpai"=>"","yijifeiyong"=>""
                    ,"erjifeiyong"=>"","channel1"=>"","fenqu"=>"","sheng"=>"","shi"=>""
            ];
        }
        //echo $where;
        $sum=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        
        ->where($where)
        ->count();

        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        ->join("cg_jxs j","ap.jingxiao=j.id","LEFT")
        ->join("cg_market_zoning z","ap.fenqu=z.id","LEFT")
        ->join("cg_market_provice p","ap.sheng=p.id","LEFT")
        ->join("cg_market_city c","ap.shi=c.id","LEFT")
         ->join("cg_channel_types1 c1","ap.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","ap.channel2=c2.id","LEFT")
        ->join("employee e","ap.people=e.id","LEFT")
        ->join("cg_system x","ap.xitong=x.id","LEFT")
        ->join("cg_net n","ap.wangdian=n.id","LEFT")
        ->join("pro_brands pb","ap.pinpai=pb.id","LEFT")
        ->join("pro_category pc","ap.pinlei=pc.id","LEFT")
        ->join("pro_series ps","ap.xilie=ps.id","LEFT")
        ->join("cg_feiyongkemu1 f1","ap.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","ap.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","ap.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","ap.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","ap.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","ap.erjikaoheqi=k2.id","LEFT")
        ->where($where)
        ->order("t.id desc,ap.id desc")
        ->limit($start,$list)
        ->field("c1.title as channel1,z.title as fenqu,p.title as sheng,c.title as shi,j.title as jingxiao,pb.brand_name as pinpai,f1.title as  yijifeiyong,f2.title as erjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,ap.feiyong,ap.detail_no,t.no,pc.category_name as pinlei,e.em_name as ename")
        ->select();
        //print_r($apply);
         $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        //所有财年
        $cainian=Db::table("cg_cainian")->select();
        $this->assign("cainian",$cainian);
        //一级考核期
        $kaohe1=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.title,k1.id")->select();
        $this->assign("kaohe1",$kaohe1);
        $jsonkaohe1=json_encode($kaohe1);
        $this->assign("jsonkaohe1",$jsonkaohe1);
         $kaohe2=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_kaoheqi2 k2","k2.pid=k1.id")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.id as yijikaoheqi,k2.title,k2.id")->select();
          $this->assign("kaohe2",$kaohe2);
           $jsonkaohe2=json_encode($kaohe2);
          $this->assign("jsonkaohe2",$jsonkaohe2);
          //print_r($kaohe2);
        //品牌
        $pinpai=Db::table("pro_brands")->where(["is_deleted"=>0])->select();
        $this->assign("pinpai",$pinpai);
        //一级费用科目
        $yijifeiyong=Db::table("cg_feiyongkemu1")->select();
        $this->assign("yijifeiyong",$yijifeiyong);
        //二级费用科目
        $erjifeiyong=Db::table("cg_feiyongkemu2")->alias("f2")
        ->join("cg_feiyongkemu1 f1","f2.pid=f1.id")
        ->field("f1.id as yijifeiyong,f2.title,f2.id")
        ->select();
        $this->assign("erjifeiyong",$erjifeiyong);
        $jsonerjifeiyong=json_encode($erjifeiyong);
        $this->assign("jsonerjifeiyong",$jsonerjifeiyong);
        //渠道
        $qudao=Db::table("cg_channel_types1")->select();
        $this->assign("qudao",$qudao);
         //分区
        $fenqu=Db::table("cg_market_zoning")->where(["isdel"=>0])->select();
        $this->assign("fenqu",$fenqu);
        //省份
        $sheng=Db::table("cg_market_provice")->alias("p")
        ->join("cg_market_zoning z","p.zoning=z.id")
        ->field("z.id as fenqu,p.title,p.id")->select();
        $this->assign("sheng",$sheng);
       
         $jsonsheng=json_encode($sheng);
          $this->assign("jsonsheng",$jsonsheng);
        //市
        $shi=Db::table("cg_market_city")->alias("c")
        ->join("cg_market_provice p","c.provice=p.id")
        ->field("p.id as sheng,c.title,c.id")->select();
        $this->assign("shi",$shi);
        $jsonshi=json_encode($shi);
          $this->assign("jsonshi",$jsonshi);

        return $this->fetch('myfayunmingxi');
    }
    //我的发运管理
    public function myfayunmanage($page=1,$list=20){
        //获取编号
        $typename="个人发运目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        //接收数据
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
        $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id and t.c_users_id=$uid ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id and t.c_users_id=$uid ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);
        if($data){
            if(trim($data["no"]))
                $where.="and t.no like '%{$data["no"]}%' ";
            if(trim($data["title"]))
                $where.="and t.title like '%{$data["title"]}%' ";
            if(trim($data["start"]))
                $where.="and t.created_on>='{$data["start"]} 00:00:00' ";
            if(trim($data["end"]))
                $where.="and t.created_on<='{$data["end"]} 23:59:59' ";
             if(trim($data["state"]))
                $where.="and t.state={$data["state"]} ";
        }else{
            $data=["no"=>"","title"=>"","start"=>"","end"=>"","state"=>0];
        }
        //数量
        $sum=Db::table("target_apply")->alias("t")->where($where)->count();
        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $apply=Db::table("target_apply")->alias("t")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("ti_state ts","t.state=ts.id")
        ->where($where)->order("created_on desc,no desc")
        ->limit($start,$list)
        ->field("t.id,t.no,t.title,t.state,ec.em_name as cname,t.created_on,ts.title as state_name,t.tag,t.state2,t.islock")
        ->select();

         for($i=0;$i<count($apply);$i++){
            $apply[$i]["created_on"]=date("Y-m-d H:i",strtotime($apply[$i]["created_on"]));
            if($apply[$i]["state2"]==1)
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>8])->value("id");
            else
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>9])->value("id");
            
        }
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        $statearr=Db::table("ti_state")->select();
        $this->assign("statearr",$statearr);

        return $this->fetch('myfayunmanage');
    }
    //未设置目标的销售人员名单
    public function myfayunnoset($page=1,$list=10){
         //获取编号
        $typename="个人发运目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        //接收数据
        $request = Request::instance();
        $data=$request->post();
         $uid=Session::get("login_id");
 $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id and t.c_users_id=$uid ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id and t.c_users_id=$uid ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);
        

        return $this->fetch('myfayunnoset');
    }
    //新增我的发运
    public function new_fayun($serid){
        $this->assign("serid",$serid);
        return $this->fetch('new_fayun');
    }
     //异动预算
    public function yd_jt_add(){
        $request = Request::instance();

        
        $data=$request->post();
        //print_r($data);exit;
        $target=Db::table("target_apply")->where(["id"=>$data['tid']])->find();
        $type_id=$target["type_id"];
           $typs="";
           switch ($type_id) {
               case 6:
                   # code...
                   $typs="个人发运目标格式";
                   break;
                case 7:
                   # code...
                   $typs="个人回款目标格式";
                   break;
                case 8:
                   # code...
                   $typs="个人零售目标格式";
                   break;
           }
        $biaoti=$target["title"];
        $remark=trim(htmlspecialchars($data["remark"]));
        $state=trim($data["type"]);
       // $shenpiren=trim($data["shenpiren"]);
        $file = request()->file('wenjian');
        $tid=$data['tid'];
      
            $fujian=$target["fujian"];
        if($file){
            $info = $file->validate(['size'=>2*1024*1024,'ext'=>'xls'])->move(ROOT_PATH . 'public/uploads/Jituan' . DS );
            if($info){
                // 成功上传后 获取上传信息
               
                // 输出
                $savename= $info->getSaveName();
                $path=ROOT_PATH . 'public/uploads/Jituan/'.$savename;
                $res=$this->excel_yidong($typs,$tid,$state,$biaoti,$remark,$path,$fujian);
                return $res;

                
            }else{
                // 上传失败获取错误信息
                return json_encode(["msg"=>$file->getError(),"state"=>2]);
               
            }
        }else{
             return json_encode(["msg"=>"文件不能为空","state"=>2]);
        }

    }
    
       public function xg_jt_add(){
        $request = Request::instance();

        
        $data=$request->post();
       // print_r($data);exit;
           $target=Db::table("target_apply")->where(["id"=>$data['tid']])->find();
           $type_id=$target["type_id"];
           $typs="";
           switch ($type_id) {
               case 6:
                   # code...
                   $typs="个人发运目标格式";
                   break;
                case 7:
                   # code...
                   $typs="个人回款目标格式";
                   break;
                case 8:
                   # code...
                   $typs="个人零售目标格式";
                   break;
           }
        $biaoti=trim($data["title"]);
        $remark=trim(htmlspecialchars($data["remark"]));
        $state=trim($data["type"]);
       // $shenpiren=trim($data["shenpiren"]);
        $file = request()->file('wenjian');
        $tid=$data['tid'];
       
        $path=null;
         if($file){
            $info = $file->validate(['size'=>2*1024*1024,'ext'=>'xls'])->move(ROOT_PATH . 'public/uploads/Jituan' . DS );
            if($info){
                // 成功上传后 获取上传信息
               
                // 输出
                $savename= $info->getSaveName();
                $path=ROOT_PATH . 'public/uploads/Jituan/'.$savename;
                $res=$this->excel_xiugai($typs,$tid,$path);
                $res=json_decode($res,true);
                
            }else{
                // 上传失败获取错误信息
                return json_encode(["msg"=>$file->getError(),"state"=>2]);
               
            }
        }else{
            $res=["state"=>1,"msg"=>""];
        }

        if($res["state"]==1){
            try{
                 if($path){
                 $res1=Db::table("target_apply")->where(["id"=>$tid])->update(["title"=>$biaoti,"remark"=>$remark,"state"=>$state,"path"=>$path]);
                }else{
                     $res1=Db::table("target_apply")->where(["id"=>$tid])->update(["title"=>$biaoti,"remark"=>$remark,"state"=>$state]);
                }
            }catch(\Exception $e){
             return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
            }
             if($state==3){
                $backjson=$this->ystijiao($tid);
                return json_encode($res);
            }else
                return json_encode($res);
        }else{
             return json_encode($res);
        }
            
            
    }
    //保存我的发运
    public function new_fayun_add(){
       $request = Request::instance();

        
        $data=$request->post();
        $biaoti=trim(htmlspecialchars($data["title"]));
        if(!$biaoti){
             return json_encode(["msg"=>"主题不能为空","state"=>2]);
        }
        $remark=trim(htmlspecialchars($data["remark"]));
        $state=trim($data["type"]);
        $file = request()->file('wenjian');
        if($data["attach"])
            $fujian=$data["attach"];
        else
            $fujian=null;

        if($file){
            $info = $file->validate(['size'=>2*1024*1024,'ext'=>'xls'])->move(ROOT_PATH . 'public/uploads/Myfayun' . DS );
            if($info){
                // 成功上传后 获取上传信息
               
                // 输出
                $savename= $info->getSaveName();
                $path=ROOT_PATH . 'public/uploads/Myfayun/'.$savename;
                $res=$this->excel_duqu("个人发运目标格式",$state,$biaoti,$remark,$path,$fujian);
                return $res;

                
            }else{
                // 上传失败获取错误信息
                return json_encode(["msg"=>$file->getError(),"state"=>2]);
               
            }
        }else{
             return json_encode(["msg"=>"文件不能为空","state"=>2]);
        }
        //echo 1;exit;
        //print_r($data);exit;
    }
    //发运详情
    public function fayundetail($tid){
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
    //发运详情
    public function lingshoudetail($tid){
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
    //发运详情
    public function huikuandetail($tid){
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
     //导出
     public function daochu($tid){
         $apply=Db::table("target_apply")->where(["id"=>$tid])->find();
         $file_path=$apply["path"];$file_name="个人目标-".$apply["title"].".xls";
         header("Content-type:text/html;charset=utf-8"); 
         
           
            if(!file_exists($file_path)){ 
            echo "没有该文件文件"; 
            return ; 
            } 
            $fp=fopen($file_path,"r"); 
            $file_size=filesize($file_path); 
            //下载文件需要用到的头 
            Header("Content-type: application/octet-stream"); 
            Header("Accept-Ranges: bytes"); 
            Header("Accept-Length:".$file_size); 
            Header("Content-Disposition: attachment; filename=".$file_name); 
            ob_clean();
            flush();
            $buffer=1024; 
            $file_count=0; 
            //向浏览器返回数据 
            while(!feof($fp) && $file_count<$file_size){ 
            $file_con=fread($fp,$buffer); 
            $file_count+=$buffer; 
            echo $file_con; 
            } 
            fclose($fp); 
     }
    //发运详情
    public function fayunxiugai($tid,$serid){
        $this->assign("serid",$serid);
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
     //发运详情
    public function lingshouxiugai($tid,$serid){
        $this->assign("serid",$serid);
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
     //发运详情
    public function huikuanxiugai($tid,$serid){
        $this->assign("serid",$serid);
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
    //发运详情
    public function fayunyidong($tid,$serid){
        $this->assign("serid",$serid);
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
    //发运详情
    public function huikuanyidong($tid,$serid){
        $this->assign("serid",$serid);
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
    //发运详情
    public function lingshouyidong($tid,$serid){
        $this->assign("serid",$serid);
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
    //发运详情
    public function fayunzuofei($serid='',$from=1,$tid){
        $this->assign("tid",$tid);$this->assign("from",$from);
        $this->assign("serid",$serid);
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
            if($apply["state2"]==1)
                $fid=8;
            else
                $fid=9;
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
          $btn=$this->getbton($fid,$tid);
        $this->assign("btn",$btn);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
    public function lingshouzuofei($serid='',$from=1,$tid){
        $this->assign("tid",$tid);$this->assign("from",$from);
        $this->assign("serid",$serid);
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
            if($apply["state2"]==1)
                $fid=8;
            else
                $fid=9;
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
          $btn=$this->getbton($fid,$tid);
        $this->assign("btn",$btn);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
    public function huikuanzuofei($serid='',$from=1,$tid){
        $this->assign("tid",$tid);$this->assign("from",$from);
        $this->assign("serid",$serid);
       $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply yuant","t.yuanid=yuant.id","LEFT")
        ->join("target_apply nextt","t.nextid=nextt.id","LEFT")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("department_employee de","ec.id=de.employee_id","LEFT")
        ->join("department d","de.department_id=d.id","LEFT")->where(["t.id"=>$tid])
        ->field("t.id,t.title,t.no,yuant.no as yuanno,nextt.no as nextno,yuant.id as yuanid,nextt.id as nextid,t.state,ec.em_name as cname,t.created_on,d.de_name,t.remark,t.state2,t.tag,t.fujian")
        ->find();
        $this->assign("apply",$apply);
          $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            $this->assign("fujian",$fujian);
            if($apply["state2"]==1)
                $fid=8;
            else
                $fid=9;
        $model=db("target_apply_detail")->where(["apply_id"=>$tid])->find();
        $this->assign("model",$model);
        //明细
        $detail=Db::table("target_apply_detail")->alias("t")
        ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","t.channel2=c2.id","LEFT")
        ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
        ->join("cg_system x","t.xitong=x.id","LEFT")
        ->join("cg_net n","t.wangdian=n.id","LEFT")
        ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
        ->join("pro_category pc","t.pinlei=pc.id","LEFT")
        ->join("pro_series ps","t.xilie=ps.id","LEFT")
        ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
        ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
        ->join("cg_market_city ac","t.shi=ac.id","LEFT")
        ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
        ->join("employee e","t.people=e.id","LEFT")
        ->where(["t.apply_id"=>$tid])
        ->field("t.detail_no,c1.title as channel1,c2.title as channel2,j.title as jingxiao,x.title as xitong,n.title as wangdian,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,e.em_name,e.em_name as ename")
        ->select();
        $this->assign("detail",$detail);
              //获取财年和考核期
        $ck=Db::table("target_apply_detail")->query("select c.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,c.id as cid,k1.id as k1id,k2.id as k2id  from target_apply_detail as t inner join cg_cainian as c on t.cainian=c.id inner join cg_kaoheqi1 as k1 on t.yijikaoheqi=k1.id LEFT join cg_kaoheqi2 as k2 on t.erjikaoheqi=k2.id where t.apply_id=$tid group by t.cainian,t.yijikaoheqi,t.erjikaoheqi,c.id,k1.id,k2.id ");
        //print_r($ck);
        for($i=0;$i<count($ck);$i++){
            if($ck[$i]["erjikaoheqi"]){
                $ck[$i]["kaoheqi"]=$ck[$i]["erjikaoheqi"];
                $ck[$i]["kid"]=$ck[$i]["k2id"];
            }else{
                $ck[$i]["kaoheqi"]=$ck[$i]["yijikaoheqi"]; $ck[$i]["kid"]=$ck[$i]["k1id"];
            }
        }
        $this->assign("ck",$ck);
        //考核期数量
        $kc=count($ck)+1;
    
        $this->assign("kc",$kc);
        //产品纬度
        if($model["xilie"]){
            $grouppro="t.xilie";
            $strpro="t.pinpai,t.pinlei,t.xilie";
        }else if($model["pinlei"]){
            $grouppro="t.pinlei";
            $strpro="t.pinpai,t.pinlei";
        }else{
            $grouppro="t.pinpai";
            
            $strpro="t.pinpai";
        }
        if($model["erjikaoheqi"]){
            $prode=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.erjikaoheqi");
        }else{
            $prode=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$strpro},sum(feiyong) as f  from target_apply_detail as t  where t.apply_id=$tid and t.pinpai is not null group by {$grouppro},t.cainian,t.yijikaoheqi");
        }   
        
        foreach ($prode as $k => $v) {
            # code...
            if($v["pinpai"])
                $prode[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $prode[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $prode[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $prode[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
        $pro=Db::table("target_apply_detail")->query("select {$strpro},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and pinpai is not null group by {$grouppro}");
        foreach ($pro as $k => $v) {
            # code...
            if($v["pinpai"])
                $pro[$k]["brand_name"]=db("pro_brands")->where(["id"=>$v["pinpai"]])->value("brand_name");
            else
                $pro[$k]["brand_name"]="";
            if(isset($v["pinlei"])){
                $pro[$k]["category_name"]=db("pro_category")->where(["id"=>$v["pinlei"]])->value("category_name");
            }
            if(isset($v["xilie"])){
                $pro[$k]["series_name"]=db("pro_series")->where(["id"=>$v["xilie"]])->value("series_name");
            }
        }
       // print_r($pro);
        $sumpro=0;
        for($i=0;$i<count($pro);$i++){
            $sumpro+=$pro[$i]["f"];
        }
        $this->assign("sumpro",$sumpro);
        $this->assign("pro",$pro);
        $this->assign("prode",$prode);

        //区域纬度
        if($model["jingxiao"]){
            $grouparea="t.jingxiao";
            $areastr="t.fenqu,t.sheng,t.shi,t.jingxiao";
        }else if($model["shi"]){
            $grouparea="t.shi";
            $areastr="t.fenqu,t.sheng,t.shi";
        }else if($model["sheng"]){
            $grouparea="t.sheng";
            $areastr="t.fenqu,t.sheng";
        }else{
            $grouparea="t.fenqu";
            $areastr="t.fenqu";
        }
        if($model["erjikaoheqi"]){
            $areade=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,erjikaoheqi");
        }else{
            $areade=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,{$areastr},sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by {$grouparea},cainian,yijikaoheqi");
        }
        

        $area=Db::table("target_apply_detail")->query("select {$areastr},sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and fenqu is not null group by {$grouparea}");
        foreach ($area as $k => $v) {
            # code...
            if($v["fenqu"]){
                $area[$k]["fenqu_name"]=db("cg_market_zoning")->where(["id"=>$v["fenqu"]])->value("title");
            }else{
                $area[$k]["fenqu_name"]="";
            }
            if(isset($v["sheng"]))
                $area[$k]["sheng_name"]=db("cg_market_provice")->where(["id"=>$v["sheng"]])->value("title");
            if(isset($v["shi"]))
                $area[$k]["shi_name"]=db("cg_market_city")->where(["id"=>$v["shi"]])->value("title");
            if(isset($v["jingxiao"]))
                $area[$k]["jingxiao_name"]=db("cg_jxs")->where(["id"=>$v["jingxiao"]])->value("title");
        }
        $sumarea=0;
        for($i=0;$i<count($area);$i++){
            $sumarea+=$area[$i]["f"];
        }
        $this->assign("sumarea",$sumarea);
        $this->assign("area",$area);
        $this->assign("areade",$areade);
        if($model["erjikaoheqi"]){
            $emde=Db::table("target_apply_detail")->query("select cainian,erjikaoheqi as yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,erjikaoheqi");
        }else{
            $emde=Db::table("target_apply_detail")->query("select cainian,yijikaoheqi,people,sum(feiyong) as f  from target_apply_detail as t where apply_id=$tid and fenqu is not null group by people,cainian,yijikaoheqi");
        }
         $em=Db::table("target_apply_detail")->query("select people,sum(feiyong) as f  from target_apply_detail as t  where apply_id=$tid and people is not null group by people");
         foreach ($em as $k => $v) {
            # code...
            if($v["people"]){
                $em[$k]["people_name"]=db("employee")->where(["id"=>$v["people"]])->value("em_name");
            }else{
                $em[$k]["people_name"]="";
            }
        }
         $sumem=0;
        for($i=0;$i<count($em);$i++){
            $sumem+=$em[$i]["f"];
        }
        $this->assign("sumem",$sumem);
        $this->assign("em",$em);
        $this->assign("emde",$emde);
        if($apply["state2"]==2){
             $yuandetail=Db::table("target_apply_detail")->alias("t")
            ->join("cg_feiyongkemu1 f1","t.yijifeiyong=f1.id","LEFT")
            ->join("cg_feiyongkemu2 f2","t.erjifeiyong=f2.id","LEFT")
            ->join("cg_feiyongkemu3 f3","t.sanjifeiyong=f3.id","LEFT")
            ->join("cg_cainian cn","t.cainian=cn.id","LEFT")
            ->join("cg_kaoheqi1 k1","t.yijikaoheqi=k1.id","LEFT")
            ->join("cg_kaoheqi2 k2","t.erjikaoheqi=k2.id","LEFT")
            ->join("pro_brands pb","t.pinpai=pb.id","LEFT")
            ->join("pro_category pc","t.pinlei=pc.id","LEFT")
            ->join("pro_series ps","t.xilie=ps.id","LEFT")
            ->join("cg_jxs j","t.jingxiao=j.id","LEFT")
            ->join("cg_market_zoning af","t.fenqu=af.id","LEFT")
            ->join("cg_market_provice ap","t.sheng=ap.id","LEFT")
            ->join("cg_market_city ac","t.shi=ac.id","LEFT")
            ->join("employee e","t.people=e.id","LEFT")
            ->join("cg_channel_types1 c1","t.channel1=c1.id","LEFT")
            ->where(["t.apply_id"=>$apply["yuanid"]])
            ->field("e.em_name as ename,c1.title as channel1,j.title as jingxiao,af.title as fenqu,ap.title as sheng,ac.title as shi,f1.title as yijifeiyong,f2.title as erjifeiyong,f3.title as sanjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,t.feiyong,pb.brand_name as pinpai,pc.category_name as pinlei,ps.series_name as xilie")
            ->select();
            $tongjidata=[];
            for($i=0;$i<count($detail);$i++){
                $tongji=$detail[$i];
                $tongji["yuanfeiyong"]=0;
                
                for($j=0;$j<count($yuandetail);$j++){
                    if($detail[$i]["yijifeiyong"]==$yuandetail[$j]["yijifeiyong"]&&$detail[$i]["erjifeiyong"]==$yuandetail[$j]["erjifeiyong"]&&$detail[$i]["sanjifeiyong"]==$yuandetail[$j]["sanjifeiyong"]&&$detail[$i]["yijikaoheqi"]==$yuandetail[$j]["yijikaoheqi"]&&$detail[$i]["erjikaoheqi"]==$yuandetail[$j]["erjikaoheqi"]&&$detail[$i]["ename"]==$yuandetail[$j]["ename"]&&$detail[$i]["cainian"]==$yuandetail[$j]["cainian"]&&$detail[$i]["fenqu"]==$yuandetail[$j]["fenqu"]&&$detail[$i]["sheng"]==$yuandetail[$j]["sheng"]&&$detail[$i]["shi"]==$yuandetail[$j]["shi"]&&$detail[$i]["jingxiao"]==$yuandetail[$j]["jingxiao"]&&$detail[$i]["pinpai"]==$yuandetail[$j]["pinpai"]&&$detail[$i]["pinlei"]==$yuandetail[$j]["pinlei"]&&$detail[$i]["xilie"]==$yuandetail[$j]["xilie"]&&$detail[$i]["channel1"]==$yuandetail[$j]["channel1"]){
                        //内容相似
                        $tongji["yuanfeiyong"]=$yuandetail[$j]["feiyong"];
                        array_splice($yuandetail, $j, 1);break;
                    }
                }
                $tongji["duibi"]=$tongji["feiyong"]-$tongji["yuanfeiyong"];
                $tongjidata[]=$tongji;
            }
            for($i=0;$i<count($yuandetail);$i++){
                $tongji=["yijifeiyong"=>$yuandetail[$i]["yijifeiyong"],"erjifeiyong"=>$yuandetail[$i]["erjifeiyong"],"sanjifeiyong"=>$yuandetail[$i]["sanjifeiyong"],"cainian"=>$yuandetail[$i]["cainian"],"yijikaoheqi"=>$yuandetail[$i]["yijikaoheqi"],"erjikaoheqi"=>$yuandetail[$i]["erjikaoheqi"],"ename"=>$yuandetail[$i]["ename"],"feiyong"=>0,"yuanfeiyong"=>$yuandetail[$i]["feiyong"],"duibi"=>0-$yuandetail[$i]["feiyong"],"fenqu"=>$yuandetail[$i]["fenqu"],"sheng"=>$yuandetail[$i]["sheng"],"shi"=>$yuandetail[$i]["shi"],"jingxiao"=>$yuandetail[$i]["jingxiao"],"pinpai"=>$yuandetail[$i]["pinpai"],"pinlei"=>$yuandetail[$i]["pinlei"],"xilie"=>$yuandetail[$i]["xilie"],"channel1"=>$yuandetail[$i]["channel1"]];
                $tongjidata[]=$tongji;
            }
            $this->assign("tongjidata",$tongjidata);
        }
         if($apply["state2"]==1)
            $logs=$this->getlog(8,$tid);
        else
             $logs=$this->getlog(9,$tid);
          $btn=$this->getbton($fid,$tid);
        $this->assign("btn",$btn);
        //print_r($logs);
        $this->assign("logs",$logs);
        return $this->fetch('');
    }
    ////我的回款
    public function myhuikuantongji($page=1,$list=20){
        //获取编号
        $typename="个人回款目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        //接收数据
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
        $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid  and t.tag<>1 ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) and t.tag<>1 ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid and t.tag<>1 ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and  t.state=7 and t.is_deleted=0 and t.type_id=$type_id and t.tag<>1 ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);

       $data=$request->post();
          if($data){
            if(trim($data["cainian"]))
                $where.="and ap.cainian='{$data["cainian"]}' ";
           if(trim($data["yijikaoheqi"]))
                $where.="and ap.yijikaoheqi='{$data["yijikaoheqi"]}' ";
            if(trim($data["erjikaoheqi"]))
                $where.="and ap.erjikaoheqi='{$data["erjikaoheqi"]}' ";
            if(trim($data["pinpai"]))
                $where.="and ap.pinpai='{$data["pinpai"]}' ";
            if(trim($data["yijifeiyong"]))
                $where.="and ap.yijifeiyong='{$data["yijifeiyong"]}' ";
            if(trim($data["erjifeiyong"]))
                $where.="and ap.erjifeiyong='{$data["erjifeiyong"]}' ";
             if(trim($data["channel1"]))
                $where.="and ap.channel1='{$data["channel1"]}' ";
             if(trim($data["fenqu"]))
                $where.="and ap.fenqu='{$data["fenqu"]}' ";
            if(trim($data["sheng"]))
                $where.="and ap.sheng='{$data["sheng"]}' ";
            if(trim($data["shi"])){
                $where.="and ap.shi='{$data["shi"]}' ";
            }
          
        }else{
            $data=["cainian"=>"","yijikaoheqi"=>"","erjikaoheqi"=>"","pinpai"=>"","yijifeiyong"=>""
                    ,"erjifeiyong"=>"","channel1"=>"","fenqu"=>"","sheng"=>"","shi"=>""
            ];
        }
        //echo $where;
        $sum=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        
        ->where($where)
        ->count();

        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        ->join("cg_jxs j","ap.jingxiao=j.id","LEFT")
        ->join("cg_market_zoning z","ap.fenqu=z.id","LEFT")
        ->join("cg_market_provice p","ap.sheng=p.id","LEFT")
        ->join("cg_market_city c","ap.shi=c.id","LEFT")
         ->join("cg_channel_types1 c1","ap.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","ap.channel2=c2.id","LEFT")
        ->join("employee e","ap.people=e.id","LEFT")
        ->join("cg_system x","ap.xitong=x.id","LEFT")
        ->join("cg_net n","ap.wangdian=n.id","LEFT")
        ->join("pro_brands pb","ap.pinpai=pb.id","LEFT")
        ->join("pro_category pc","ap.pinlei=pc.id","LEFT")
        ->join("pro_series ps","ap.xilie=ps.id","LEFT")
        ->join("cg_feiyongkemu1 f1","ap.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","ap.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","ap.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","ap.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","ap.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","ap.erjikaoheqi=k2.id","LEFT")
        ->where($where)
        ->order("t.id desc,ap.id desc")
        ->limit($start,$list)
        ->field("c1.title as channel1,z.title as fenqu,p.title as sheng,c.title as shi,j.title as jingxiao,pb.brand_name as pinpai,f1.title as  yijifeiyong,f2.title as erjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,ap.feiyong,ap.detail_no,t.no,pc.category_name as pinlei,e.em_name as ename")
        ->select();
        //print_r($apply);
         $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        //所有财年
        $cainian=Db::table("cg_cainian")->select();
        $this->assign("cainian",$cainian);
        //一级考核期
        $kaohe1=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.title,k1.id")->select();
        $this->assign("kaohe1",$kaohe1);
        $jsonkaohe1=json_encode($kaohe1);
        $this->assign("jsonkaohe1",$jsonkaohe1);
         $kaohe2=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_kaoheqi2 k2","k2.pid=k1.id")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.id as yijikaoheqi,k2.title,k2.id")->select();
          $this->assign("kaohe2",$kaohe2);
           $jsonkaohe2=json_encode($kaohe2);
          $this->assign("jsonkaohe2",$jsonkaohe2);
          //print_r($kaohe2);
        //品牌
        $pinpai=Db::table("pro_brands")->where(["is_deleted"=>0])->select();
        $this->assign("pinpai",$pinpai);
        //一级费用科目
        $yijifeiyong=Db::table("cg_feiyongkemu1")->select();
        $this->assign("yijifeiyong",$yijifeiyong);
        //二级费用科目
        $erjifeiyong=Db::table("cg_feiyongkemu2")->alias("f2")
        ->join("cg_feiyongkemu1 f1","f2.pid=f1.id")
        ->field("f1.id as yijifeiyong,f2.title,f2.id")
        ->select();
        $this->assign("erjifeiyong",$erjifeiyong);
        $jsonerjifeiyong=json_encode($erjifeiyong);
        $this->assign("jsonerjifeiyong",$jsonerjifeiyong);
        //渠道
        $qudao=Db::table("cg_channel_types1")->select();
        $this->assign("qudao",$qudao);
         //分区
        $fenqu=Db::table("cg_market_zoning")->where(["isdel"=>0])->select();
        $this->assign("fenqu",$fenqu);
        //省份
        $sheng=Db::table("cg_market_provice")->alias("p")
        ->join("cg_market_zoning z","p.zoning=z.id")
        ->field("z.id as fenqu,p.title,p.id")->select();
        $this->assign("sheng",$sheng);
       
         $jsonsheng=json_encode($sheng);
          $this->assign("jsonsheng",$jsonsheng);
        //市
        $shi=Db::table("cg_market_city")->alias("c")
        ->join("cg_market_provice p","c.provice=p.id")
        ->field("p.id as sheng,c.title,c.id")->select();
        $this->assign("shi",$shi);
        $jsonshi=json_encode($shi);
          $this->assign("jsonshi",$jsonshi);
        return $this->fetch('myhuikuantongji');
    }
    //我的回款明细
    
    public function myhuikuanmingxi($page=1,$list=20){
        //获取编号
        $typename="个人回款目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        //接收数据
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
        $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid  and t.tag<>1 ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) and t.tag<>1 ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid and t.tag<>1 ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and  t.state=7 and t.is_deleted=0 and t.type_id=$type_id and t.tag<>1 ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);

       $data=$request->post();
          if($data){
            if(trim($data["cainian"]))
                $where.="and ap.cainian='{$data["cainian"]}' ";
           if(trim($data["yijikaoheqi"]))
                $where.="and ap.yijikaoheqi='{$data["yijikaoheqi"]}' ";
            if(trim($data["erjikaoheqi"]))
                $where.="and ap.erjikaoheqi='{$data["erjikaoheqi"]}' ";
            if(trim($data["pinpai"]))
                $where.="and ap.pinpai='{$data["pinpai"]}' ";
            if(trim($data["yijifeiyong"]))
                $where.="and ap.yijifeiyong='{$data["yijifeiyong"]}' ";
            if(trim($data["erjifeiyong"]))
                $where.="and ap.erjifeiyong='{$data["erjifeiyong"]}' ";
             if(trim($data["channel1"]))
                $where.="and ap.channel1='{$data["channel1"]}' ";
             if(trim($data["fenqu"]))
                $where.="and ap.fenqu='{$data["fenqu"]}' ";
            if(trim($data["sheng"]))
                $where.="and ap.sheng='{$data["sheng"]}' ";
            if(trim($data["shi"])){
                $where.="and ap.shi='{$data["shi"]}' ";
            }
          
        }else{
            $data=["cainian"=>"","yijikaoheqi"=>"","erjikaoheqi"=>"","pinpai"=>"","yijifeiyong"=>""
                    ,"erjifeiyong"=>"","channel1"=>"","fenqu"=>"","sheng"=>"","shi"=>""
            ];
        }
        //echo $where;
        $sum=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        
        ->where($where)
        ->count();

        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        ->join("cg_jxs j","ap.jingxiao=j.id","LEFT")
        ->join("cg_market_zoning z","ap.fenqu=z.id","LEFT")
        ->join("cg_market_provice p","ap.sheng=p.id","LEFT")
        ->join("cg_market_city c","ap.shi=c.id","LEFT")
         ->join("cg_channel_types1 c1","ap.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","ap.channel2=c2.id","LEFT")
        ->join("employee e","ap.people=e.id","LEFT")
        ->join("cg_system x","ap.xitong=x.id","LEFT")
        ->join("cg_net n","ap.wangdian=n.id","LEFT")
        ->join("pro_brands pb","ap.pinpai=pb.id","LEFT")
        ->join("pro_category pc","ap.pinlei=pc.id","LEFT")
        ->join("pro_series ps","ap.xilie=ps.id","LEFT")
        ->join("cg_feiyongkemu1 f1","ap.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","ap.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","ap.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","ap.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","ap.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","ap.erjikaoheqi=k2.id","LEFT")
        ->where($where)
        ->order("t.id desc,ap.id desc")
        ->limit($start,$list)
        ->field("c1.title as channel1,z.title as fenqu,p.title as sheng,c.title as shi,j.title as jingxiao,pb.brand_name as pinpai,f1.title as  yijifeiyong,f2.title as erjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,ap.feiyong,ap.detail_no,t.no,pc.category_name as pinlei,e.em_name as ename")
        ->select();
        //print_r($apply);
         $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        //所有财年
        $cainian=Db::table("cg_cainian")->select();
        $this->assign("cainian",$cainian);
        //一级考核期
        $kaohe1=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.title,k1.id")->select();
        $this->assign("kaohe1",$kaohe1);
        $jsonkaohe1=json_encode($kaohe1);
        $this->assign("jsonkaohe1",$jsonkaohe1);
         $kaohe2=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_kaoheqi2 k2","k2.pid=k1.id")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.id as yijikaoheqi,k2.title,k2.id")->select();
          $this->assign("kaohe2",$kaohe2);
           $jsonkaohe2=json_encode($kaohe2);
          $this->assign("jsonkaohe2",$jsonkaohe2);
          //print_r($kaohe2);
        //品牌
        $pinpai=Db::table("pro_brands")->where(["is_deleted"=>0])->select();
        $this->assign("pinpai",$pinpai);
        //一级费用科目
        $yijifeiyong=Db::table("cg_feiyongkemu1")->select();
        $this->assign("yijifeiyong",$yijifeiyong);
        //二级费用科目
        $erjifeiyong=Db::table("cg_feiyongkemu2")->alias("f2")
        ->join("cg_feiyongkemu1 f1","f2.pid=f1.id")
        ->field("f1.id as yijifeiyong,f2.title,f2.id")
        ->select();
        $this->assign("erjifeiyong",$erjifeiyong);
        $jsonerjifeiyong=json_encode($erjifeiyong);
        $this->assign("jsonerjifeiyong",$jsonerjifeiyong);
        //渠道
        $qudao=Db::table("cg_channel_types1")->select();
        $this->assign("qudao",$qudao);
         //分区
        $fenqu=Db::table("cg_market_zoning")->where(["isdel"=>0])->select();
        $this->assign("fenqu",$fenqu);
        //省份
        $sheng=Db::table("cg_market_provice")->alias("p")
        ->join("cg_market_zoning z","p.zoning=z.id")
        ->field("z.id as fenqu,p.title,p.id")->select();
        $this->assign("sheng",$sheng);
       
         $jsonsheng=json_encode($sheng);
          $this->assign("jsonsheng",$jsonsheng);
        //市
        $shi=Db::table("cg_market_city")->alias("c")
        ->join("cg_market_provice p","c.provice=p.id")
        ->field("p.id as sheng,c.title,c.id")->select();
        $this->assign("shi",$shi);
        $jsonshi=json_encode($shi);
          $this->assign("jsonshi",$jsonshi);

        return $this->fetch('myhuikuanmingxi');
    }
    //我的回款管理
    public function myhuikuanmanage($page=1,$list=10){
        //获取编号
        $typename="个人回款目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
        $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id and t.c_users_id=$uid ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id and t.c_users_id=$uid ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);
        if($data){
            if(trim($data["no"]))
                $where.="and t.no like '%{$data["no"]}%' ";
            if(trim($data["title"]))
                $where.="and t.title like '%{$data["title"]}%' ";
            if(trim($data["start"]))
                $where.="and t.created_on>='{$data["start"]} 00:00:00' ";
            if(trim($data["end"]))
                $where.="and t.created_on<='{$data["end"]} 23:59:59' ";
             if(trim($data["state"]))
                $where.="and t.state={$data["state"]} ";
        }else{
            $data=["no"=>"","title"=>"","start"=>"","end"=>"","state"=>0];
        }
        //数量
        $sum=Db::table("target_apply")->alias("t")->where($where)->count();
        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $apply=Db::table("target_apply")->alias("t")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("ti_state ts","t.state=ts.id")
        ->where($where)->order("created_on desc,no desc")
        ->limit($start,$list)
        ->field("t.id,t.no,t.title,t.state,ec.em_name as cname,t.created_on,ts.title as state_name,t.tag,t.state2,t.islock")
        ->select();

         for($i=0;$i<count($apply);$i++){
            $apply[$i]["created_on"]=date("Y-m-d H:i",strtotime($apply[$i]["created_on"]));
            if($apply[$i]["state2"]==1)
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>8])->value("id");
            else
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>9])->value("id");
            
        }
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        $statearr=Db::table("ti_state")->select();
        $this->assign("statearr",$statearr);
        return $this->fetch('myhuikuanmanage');
    }

     //新增我的回款
    public function new_huikuan($serid){
         $this->assign("serid",$serid);
        return $this->fetch('new_huikuan');
    }
    //保存我的回款
    public function new_huikuan_add(){
        $request = Request::instance();

        
        $data=$request->post();
        $biaoti=trim(htmlspecialchars($data["title"]));
        if(!$biaoti){
             return json_encode(["msg"=>"主题不能为空","state"=>2]);
        }
        $remark=trim(htmlspecialchars($data["remark"]));
        $state=trim($data["type"]);
        $file = request()->file('wenjian');
        if($data["attach"])
            $fujian=$data["attach"];
        else
            $fujian=null;

        if($file){
            $info = $file->validate(['size'=>2*1024*1024,'ext'=>'xls'])->move(ROOT_PATH . 'public/uploads/Myfayun' . DS );
            if($info){
                // 成功上传后 获取上传信息
               
                // 输出
                $savename= $info->getSaveName();
                $path=ROOT_PATH . 'public/uploads/Myfayun/'.$savename;
                $res=$this->excel_duqu("个人回款目标格式",$state,$biaoti,$remark,$path,$fujian);
                return $res;

                
            }else{
                // 上传失败获取错误信息
                return json_encode(["msg"=>$file->getError(),"state"=>2]);
               
            }
        }else{
             return json_encode(["msg"=>"文件不能为空","state"=>2]);
        }
        //echo 1;exit;
        //print_r($data);exit;
    }
     //未设置目标的销售人员名单
    public function myhuikuannoset($page=1,$list=10){
         //获取编号
        $typename="个人回款目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        //接收数据
        $request = Request::instance();
        $data=$request->post();
         $uid=Session::get("login_id");
 $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id and t.c_users_id=$uid ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id and t.c_users_id=$uid ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);
        

        return $this->fetch('myhuikuannoset');
    }
    

    //////个人零售
    
      public function mylingshoutongji($page=1,$list=20){
        //获取编号
        $typename="个人零售目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        //接收数据
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
        $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid  and t.tag<>1 ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) and t.tag<>1 ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid and t.tag<>1 ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and  t.state=7 and t.is_deleted=0 and t.type_id=$type_id and t.tag<>1 ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);

       $data=$request->post();
          if($data){
            if(trim($data["cainian"]))
                $where.="and ap.cainian='{$data["cainian"]}' ";
           if(trim($data["yijikaoheqi"]))
                $where.="and ap.yijikaoheqi='{$data["yijikaoheqi"]}' ";
            if(trim($data["erjikaoheqi"]))
                $where.="and ap.erjikaoheqi='{$data["erjikaoheqi"]}' ";
            if(trim($data["pinpai"]))
                $where.="and ap.pinpai='{$data["pinpai"]}' ";
            if(trim($data["yijifeiyong"]))
                $where.="and ap.yijifeiyong='{$data["yijifeiyong"]}' ";
            if(trim($data["erjifeiyong"]))
                $where.="and ap.erjifeiyong='{$data["erjifeiyong"]}' ";
             if(trim($data["channel1"]))
                $where.="and ap.channel1='{$data["channel1"]}' ";
             if(trim($data["fenqu"]))
                $where.="and ap.fenqu='{$data["fenqu"]}' ";
            if(trim($data["sheng"]))
                $where.="and ap.sheng='{$data["sheng"]}' ";
            if(trim($data["shi"])){
                $where.="and ap.shi='{$data["shi"]}' ";
            }
          
        }else{
            $data=["cainian"=>"","yijikaoheqi"=>"","erjikaoheqi"=>"","pinpai"=>"","yijifeiyong"=>""
                    ,"erjifeiyong"=>"","channel1"=>"","fenqu"=>"","sheng"=>"","shi"=>""
            ];
        }
        //echo $where;
        $sum=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        
        ->where($where)
        ->count();

        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        ->join("cg_jxs j","ap.jingxiao=j.id","LEFT")
        ->join("cg_market_zoning z","ap.fenqu=z.id","LEFT")
        ->join("cg_market_provice p","ap.sheng=p.id","LEFT")
        ->join("cg_market_city c","ap.shi=c.id","LEFT")
         ->join("cg_channel_types1 c1","ap.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","ap.channel2=c2.id","LEFT")
        ->join("employee e","ap.people=e.id","LEFT")
        ->join("cg_system x","ap.xitong=x.id","LEFT")
        ->join("cg_net n","ap.wangdian=n.id","LEFT")
        ->join("pro_brands pb","ap.pinpai=pb.id","LEFT")
        ->join("pro_category pc","ap.pinlei=pc.id","LEFT")
        ->join("pro_series ps","ap.xilie=ps.id","LEFT")
        ->join("cg_feiyongkemu1 f1","ap.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","ap.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","ap.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","ap.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","ap.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","ap.erjikaoheqi=k2.id","LEFT")
        ->where($where)
        ->order("t.id desc,ap.id desc")
        ->limit($start,$list)
        ->field("c1.title as channel1,z.title as fenqu,p.title as sheng,c.title as shi,j.title as jingxiao,pb.brand_name as pinpai,f1.title as  yijifeiyong,f2.title as erjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,ap.feiyong,ap.detail_no,t.no,pc.category_name as pinlei,e.em_name as ename")
        ->select();
        //print_r($apply);
         $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        //所有财年
        $cainian=Db::table("cg_cainian")->select();
        $this->assign("cainian",$cainian);
        //一级考核期
        $kaohe1=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.title,k1.id")->select();
        $this->assign("kaohe1",$kaohe1);
        $jsonkaohe1=json_encode($kaohe1);
        $this->assign("jsonkaohe1",$jsonkaohe1);
         $kaohe2=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_kaoheqi2 k2","k2.pid=k1.id")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.id as yijikaoheqi,k2.title,k2.id")->select();
          $this->assign("kaohe2",$kaohe2);
           $jsonkaohe2=json_encode($kaohe2);
          $this->assign("jsonkaohe2",$jsonkaohe2);
          //print_r($kaohe2);
        //品牌
        $pinpai=Db::table("pro_brands")->where(["is_deleted"=>0])->select();
        $this->assign("pinpai",$pinpai);
        //一级费用科目
        $yijifeiyong=Db::table("cg_feiyongkemu1")->select();
        $this->assign("yijifeiyong",$yijifeiyong);
        //二级费用科目
        $erjifeiyong=Db::table("cg_feiyongkemu2")->alias("f2")
        ->join("cg_feiyongkemu1 f1","f2.pid=f1.id")
        ->field("f1.id as yijifeiyong,f2.title,f2.id")
        ->select();
        $this->assign("erjifeiyong",$erjifeiyong);
        $jsonerjifeiyong=json_encode($erjifeiyong);
        $this->assign("jsonerjifeiyong",$jsonerjifeiyong);
        //渠道
        $qudao=Db::table("cg_channel_types1")->select();
        $this->assign("qudao",$qudao);
         //分区
        $fenqu=Db::table("cg_market_zoning")->where(["isdel"=>0])->select();
        $this->assign("fenqu",$fenqu);
        //省份
        $sheng=Db::table("cg_market_provice")->alias("p")
        ->join("cg_market_zoning z","p.zoning=z.id")
        ->field("z.id as fenqu,p.title,p.id")->select();
        $this->assign("sheng",$sheng);
       
         $jsonsheng=json_encode($sheng);
          $this->assign("jsonsheng",$jsonsheng);
        //市
        $shi=Db::table("cg_market_city")->alias("c")
        ->join("cg_market_provice p","c.provice=p.id")
        ->field("p.id as sheng,c.title,c.id")->select();
        $this->assign("shi",$shi);
        $jsonshi=json_encode($shi);
          $this->assign("jsonshi",$jsonshi);
        return $this->fetch('mylingshoutongji');
    }

     //我的零售明细
    
    public function mylingshoumingxi($page=1,$list=20){
        //获取编号
        $typename="个人零售目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
       //接收数据
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
        $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid  and t.tag<>1 ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) and t.tag<>1 ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0  and  t.state=7 and t.type_id=$type_id and t.c_users_id=$uid and t.tag<>1 ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and  t.state=7 and t.is_deleted=0 and t.type_id=$type_id and t.tag<>1 ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);

       $data=$request->post();
          if($data){
            if(trim($data["cainian"]))
                $where.="and ap.cainian='{$data["cainian"]}' ";
           if(trim($data["yijikaoheqi"]))
                $where.="and ap.yijikaoheqi='{$data["yijikaoheqi"]}' ";
            if(trim($data["erjikaoheqi"]))
                $where.="and ap.erjikaoheqi='{$data["erjikaoheqi"]}' ";
            if(trim($data["pinpai"]))
                $where.="and ap.pinpai='{$data["pinpai"]}' ";
            if(trim($data["yijifeiyong"]))
                $where.="and ap.yijifeiyong='{$data["yijifeiyong"]}' ";
            if(trim($data["erjifeiyong"]))
                $where.="and ap.erjifeiyong='{$data["erjifeiyong"]}' ";
             if(trim($data["channel1"]))
                $where.="and ap.channel1='{$data["channel1"]}' ";
             if(trim($data["fenqu"]))
                $where.="and ap.fenqu='{$data["fenqu"]}' ";
            if(trim($data["sheng"]))
                $where.="and ap.sheng='{$data["sheng"]}' ";
            if(trim($data["shi"])){
                $where.="and ap.shi='{$data["shi"]}' ";
            }
          
        }else{
            $data=["cainian"=>"","yijikaoheqi"=>"","erjikaoheqi"=>"","pinpai"=>"","yijifeiyong"=>""
                    ,"erjifeiyong"=>"","channel1"=>"","fenqu"=>"","sheng"=>"","shi"=>""
            ];
        }
        //echo $where;
        $sum=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        
        ->where($where)
        ->count();

        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $apply=Db::table("target_apply")->alias("t")
        ->join("target_apply_detail ap","ap.apply_id=t.id")
        ->join("cg_jxs j","ap.jingxiao=j.id","LEFT")
        ->join("cg_market_zoning z","ap.fenqu=z.id","LEFT")
        ->join("cg_market_provice p","ap.sheng=p.id","LEFT")
        ->join("cg_market_city c","ap.shi=c.id","LEFT")
         ->join("cg_channel_types1 c1","ap.channel1=c1.id","LEFT")
        ->join("cg_channel_types2 c2","ap.channel2=c2.id","LEFT")
        ->join("employee e","ap.people=e.id","LEFT")
        ->join("cg_system x","ap.xitong=x.id","LEFT")
        ->join("cg_net n","ap.wangdian=n.id","LEFT")
        ->join("pro_brands pb","ap.pinpai=pb.id","LEFT")
        ->join("pro_category pc","ap.pinlei=pc.id","LEFT")
        ->join("pro_series ps","ap.xilie=ps.id","LEFT")
        ->join("cg_feiyongkemu1 f1","ap.yijifeiyong=f1.id","LEFT")
        ->join("cg_feiyongkemu2 f2","ap.erjifeiyong=f2.id","LEFT")
        ->join("cg_feiyongkemu3 f3","ap.sanjifeiyong=f3.id","LEFT")
        ->join("cg_cainian cn","ap.cainian=cn.id","LEFT")
        ->join("cg_kaoheqi1 k1","ap.yijikaoheqi=k1.id","LEFT")
        ->join("cg_kaoheqi2 k2","ap.erjikaoheqi=k2.id","LEFT")
        ->where($where)
        ->order("t.id desc,ap.id desc")
        ->limit($start,$list)
        ->field("c1.title as channel1,z.title as fenqu,p.title as sheng,c.title as shi,j.title as jingxiao,pb.brand_name as pinpai,f1.title as  yijifeiyong,f2.title as erjifeiyong,cn.title as cainian,k1.title as yijikaoheqi,k2.title as erjikaoheqi,ap.feiyong,ap.detail_no,t.no,pc.category_name as pinlei,e.em_name as ename")
        ->select();
        //print_r($apply);
         $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        //所有财年
        $cainian=Db::table("cg_cainian")->select();
        $this->assign("cainian",$cainian);
        //一级考核期
        $kaohe1=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.title,k1.id")->select();
        $this->assign("kaohe1",$kaohe1);
        $jsonkaohe1=json_encode($kaohe1);
        $this->assign("jsonkaohe1",$jsonkaohe1);
         $kaohe2=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_kaoheqi2 k2","k2.pid=k1.id")->join("cg_cainian c","k1.cid=c.id")
        ->field("c.id as cainian,k1.id as yijikaoheqi,k2.title,k2.id")->select();
          $this->assign("kaohe2",$kaohe2);
           $jsonkaohe2=json_encode($kaohe2);
          $this->assign("jsonkaohe2",$jsonkaohe2);
          //print_r($kaohe2);
        //品牌
        $pinpai=Db::table("pro_brands")->where(["is_deleted"=>0])->select();
        $this->assign("pinpai",$pinpai);
        //一级费用科目
        $yijifeiyong=Db::table("cg_feiyongkemu1")->select();
        $this->assign("yijifeiyong",$yijifeiyong);
        //二级费用科目
        $erjifeiyong=Db::table("cg_feiyongkemu2")->alias("f2")
        ->join("cg_feiyongkemu1 f1","f2.pid=f1.id")
        ->field("f1.id as yijifeiyong,f2.title,f2.id")
        ->select();
        $this->assign("erjifeiyong",$erjifeiyong);
        $jsonerjifeiyong=json_encode($erjifeiyong);
        $this->assign("jsonerjifeiyong",$jsonerjifeiyong);
        //渠道
        $qudao=Db::table("cg_channel_types1")->select();
        $this->assign("qudao",$qudao);
         //分区
        $fenqu=Db::table("cg_market_zoning")->where(["isdel"=>0])->select();
        $this->assign("fenqu",$fenqu);
        //省份
        $sheng=Db::table("cg_market_provice")->alias("p")
        ->join("cg_market_zoning z","p.zoning=z.id")
        ->field("z.id as fenqu,p.title,p.id")->select();
        $this->assign("sheng",$sheng);
       
         $jsonsheng=json_encode($sheng);
          $this->assign("jsonsheng",$jsonsheng);
        //市
        $shi=Db::table("cg_market_city")->alias("c")
        ->join("cg_market_provice p","c.provice=p.id")
        ->field("p.id as sheng,c.title,c.id")->select();
        $this->assign("shi",$shi);
        $jsonshi=json_encode($shi);
          $this->assign("jsonshi",$jsonshi);
      

        return $this->fetch('mylingshoumingxi');
    }
     //我的零售管理
    public function mylingshoumanage($page=1,$list=10){
        //获取编号
        $typename="个人零售目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
        $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id and t.c_users_id=$uid ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id and t.c_users_id=$uid ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);
        if($data){
            if(trim($data["no"]))
                $where.="and t.no like '%{$data["no"]}%' ";
            if(trim($data["title"]))
                $where.="and t.title like '%{$data["title"]}%' ";
            if(trim($data["start"]))
                $where.="and t.created_on>='{$data["start"]} 00:00:00' ";
            if(trim($data["end"]))
                $where.="and t.created_on<='{$data["end"]} 23:59:59' ";
             if(trim($data["state"]))
                $where.="and t.state={$data["state"]} ";
        }else{
            $data=["no"=>"","title"=>"","start"=>"","end"=>"","state"=>0];
        }
        //数量
        $sum=Db::table("target_apply")->alias("t")->where($where)->count();
        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $apply=Db::table("target_apply")->alias("t")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("ti_state ts","t.state=ts.id")
        ->where($where)->order("created_on desc,no desc")
        ->limit($start,$list)
        ->field("t.id,t.no,t.title,t.state,ec.em_name as cname,t.created_on,ts.title as state_name,t.tag,t.state2,t.islock")
        ->select();

         for($i=0;$i<count($apply);$i++){
            $apply[$i]["created_on"]=date("Y-m-d H:i",strtotime($apply[$i]["created_on"]));
            if($apply[$i]["state2"]==1)
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>8])->value("id");
            else
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>9])->value("id");
            
        }
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        $statearr=Db::table("ti_state")->select();
        $this->assign("statearr",$statearr);
        return $this->fetch('mylingshoumanage');
    }

      //新增我的回款
    public function new_lingshou($serid){
         $this->assign("serid",$serid);
        return $this->fetch('new_lingshou');
    }
    //保存我的回款
    public function new_lingshou_add(){
        $request = Request::instance();

        
        $data=$request->post();
        $biaoti=trim(htmlspecialchars($data["title"]));
        if(!$biaoti){
             return json_encode(["msg"=>"主题不能为空","state"=>2]);
        }
        $remark=trim(htmlspecialchars($data["remark"]));
        $state=trim($data["type"]);
        $file = request()->file('wenjian');
        if($data["attach"])
            $fujian=$data["attach"];
        else
            $fujian=null;

        if($file){
            $info = $file->validate(['size'=>2*1024*1024,'ext'=>'xls'])->move(ROOT_PATH . 'public/uploads/Myfayun' . DS );
            if($info){
                // 成功上传后 获取上传信息
               
                // 输出
                $savename= $info->getSaveName();
                $path=ROOT_PATH . 'public/uploads/Myfayun/'.$savename;
                $res=$this->excel_duqu("个人零售目标格式",$state,$biaoti,$remark,$path,$fujian);
                return $res;

                
            }else{
                // 上传失败获取错误信息
                return json_encode(["msg"=>$file->getError(),"state"=>2]);
               
            }
        }else{
             return json_encode(["msg"=>"文件不能为空","state"=>2]);
        }
        //echo 1;exit;
        //print_r($data);exit;
    }
      //未设置目标的销售人员名单
    public function mylingshounoset($page=1,$list=10){
         //获取编号
        $typename="个人零售目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        //接收数据
        $request = Request::instance();
        $data=$request->post();
 $uid=Session::get("login_id");
        $getdata=$request->get();
        if(!$getdata){
            $getdata["serid"]=1;
        }
        switch ($getdata["serid"]) {
            case '1':
                # 个人
                 $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id and t.c_users_id=$uid ";
                break;
            case '2':
                #下属
                $emp=Db::table("job_employee")->where(["employee_id"=>$uid])->find();
                $jid=$emp["id"];
                $uarray=Db::table("job_employee")->alias("je")->join("job j","je.job_id=j.id")->join("users u","u.employee_id=je.employee_id")->where("j.path like '[{$jid}]'")->field("u.id")->find();
                $tmparr=[];
                for($i=0;$i<count($uarray);$i++){
                    $tmparr[]=$uarray[$i]["id"];
                }
                if($tmparr){
                    $str=implode(',', $uarray);
                    $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id and (t.c_users_id=$uid or t.c_users_id in ($str)) ";
                }else{
                     $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id and t.c_users_id=$uid ";
                }
            break;


            default:
                # 全部
                $where="t.is_actived=1 and t.is_deleted=0 and t.state<>2 and t.type_id=$type_id ";
                break;
        }
        
        $this->assign("serid",$getdata["serid"]);
        

        return $this->fetch('mylingshounoset');
    }
     
    ////所有发运查询
    public function fayuncha($list=20,$page=1){
        //获取编号
        $typename="个人发运目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        //接收数据
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
     
        $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id ";
       
       
        if($data){
            if(trim($data["no"]))
                $where.="and t.no like '%{$data["no"]}%' ";
            if(trim($data["title"]))
                $where.="and t.title like '%{$data["title"]}%' ";
            if(trim($data["start"]))
                $where.="and t.created_on>='{$data["start"]} 00:00:00' ";
            if(trim($data["end"]))
                $where.="and t.created_on<='{$data["end"]} 23:59:59' ";
             if(trim($data["state"]))
                $where.="and t.state={$data["state"]} ";
        }else{
            $data=["no"=>"","title"=>"","start"=>"","end"=>"","state"=>0];
        }
        //数量
        $sum=Db::table("target_apply")->alias("t")->where($where)->count();
        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $apply=Db::table("target_apply")->alias("t")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("ti_state ts","t.state=ts.id")
        ->where($where)->order("created_on desc,no desc")
        ->limit($start,$list)
        ->field("t.id,t.no,t.title,t.state,ec.em_name as cname,t.created_on,ts.title as state_name,t.tag,t.state2,t.islock")
        ->select();

         for($i=0;$i<count($apply);$i++){
            $apply[$i]["created_on"]=date("Y-m-d H:i",strtotime($apply[$i]["created_on"]));
            if($apply[$i]["state2"]==1)
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>8])->value("id");
            else
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>9])->value("id");
            
        }
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        $statearr=Db::table("ti_state")->select();
        $this->assign("statearr",$statearr);

        return $this->fetch('fayuncha');
    }
    ////所有回款查询
    public function huikuancha($list=10,$page=1){
        //获取编号
        $typename="个人回款目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
        //接收数据
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
     
        $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id ";
       
       
        if($data){
            if(trim($data["no"]))
                $where.="and t.no like '%{$data["no"]}%' ";
            if(trim($data["title"]))
                $where.="and t.title like '%{$data["title"]}%' ";
            if(trim($data["start"]))
                $where.="and t.created_on>='{$data["start"]} 00:00:00' ";
            if(trim($data["end"]))
                $where.="and t.created_on<='{$data["end"]} 23:59:59' ";
             if(trim($data["state"]))
                $where.="and t.state={$data["state"]} ";
        }else{
            $data=["no"=>"","title"=>"","start"=>"","end"=>"","state"=>0];
        }
        //数量
        $sum=Db::table("target_apply")->alias("t")->where($where)->count();
        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $apply=Db::table("target_apply")->alias("t")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("ti_state ts","t.state=ts.id")
        ->where($where)->order("created_on desc,no desc")
        ->limit($start,$list)
        ->field("t.id,t.no,t.title,t.state,ec.em_name as cname,t.created_on,ts.title as state_name,t.tag,t.state2,t.islock")
        ->select();

         for($i=0;$i<count($apply);$i++){
            $apply[$i]["created_on"]=date("Y-m-d H:i",strtotime($apply[$i]["created_on"]));
            if($apply[$i]["state2"]==1)
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>8])->value("id");
            else
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>9])->value("id");
            
        }
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        $statearr=Db::table("ti_state")->select();
        $this->assign("statearr",$statearr);
        return $this->fetch('huikuancha');
    }
    ////所有零售查询
    public function lingshoucha($list=10,$page=1){
        //获取编号
        $typename="个人零售目标格式";
        $tmp=Db::table("target_format_type")->where(["format_type"=>$typename])->find();
        $type_id=$tmp["id"];
         //接收数据
        $request = Request::instance();
        $data=$request->post();
        $uid=Session::get("login_id");
     
        $where="t.is_actived=1 and t.is_deleted=0 and t.type_id=$type_id ";
       
       
        if($data){
            if(trim($data["no"]))
                $where.="and t.no like '%{$data["no"]}%' ";
            if(trim($data["title"]))
                $where.="and t.title like '%{$data["title"]}%' ";
            if(trim($data["start"]))
                $where.="and t.created_on>='{$data["start"]} 00:00:00' ";
            if(trim($data["end"]))
                $where.="and t.created_on<='{$data["end"]} 23:59:59' ";
             if(trim($data["state"]))
                $where.="and t.state={$data["state"]} ";
        }else{
            $data=["no"=>"","title"=>"","start"=>"","end"=>"","state"=>0];
        }
        //数量
        $sum=Db::table("target_apply")->alias("t")->where($where)->count();
        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $apply=Db::table("target_apply")->alias("t")
        ->join("users uc","t.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("ti_state ts","t.state=ts.id")
        ->where($where)->order("created_on desc,no desc")
        ->limit($start,$list)
        ->field("t.id,t.no,t.title,t.state,ec.em_name as cname,t.created_on,ts.title as state_name,t.tag,t.state2,t.islock")
        ->select();

         for($i=0;$i<count($apply);$i++){
            $apply[$i]["created_on"]=date("Y-m-d H:i",strtotime($apply[$i]["created_on"]));
            if($apply[$i]["state2"]==1)
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>8])->value("id");
            else
                $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>9])->value("id");
            
        }
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
        $this->assign("apply",$apply);
        $statearr=Db::table("ti_state")->select();
        $this->assign("statearr",$statearr);
        return $this->fetch('lingshoucha');
    }
}